/*eslint no-unused-vars: "warn"*/
const CateController = {}
const queryPromise = require(`../model/query-promise.js`)
const { dateFormat, getNowDate } = require(`../util/tool.js`)

// 分类列表
CateController.list = (req, res) => {
    let userInfo = JSON.parse(req.session.userInfo || `{}`);
    res.render(`cate-list.html`, { userInfo })
}

// 分类编辑页面(进行数据回显)//展示编辑分类的页面
CateController.edit = async (req, res) => {
    let {cat_id} = req.query;
    let sql = `select * from category where cate_id = ${cat_id}`
    let data = await queryPromise(sql)
    let userInfo = JSON.parse(req.session.userInfo || `{}`);
    res.render(`editCate.html`,{userInfo,detailData:data[0]})
}

// 分类编辑提交数据（更新分类数据）
CateController.updateCate = async (req, res) => {
    const { cate_id, cate_name,is_show} = req.body;
    //判断类名是否被占用，排除自己
    let sql = `select cate_id from category where cate_name ='${cate_name}' and cate_id !=${cate_id}`;
    let result = await queryPromise(sql)
    if (result.length) {
        // 占用
        res.json({
            message: `分类名被占用`,
            code: -1
        })
        return;
    }
    let getTime = dateFormat();
    let sql2 = `update category set cate_name ='${cate_name}',is_show = ${is_show},update_date = '${getTime}'
    where cate_id = ${cate_id}`;
    let result2 = await queryPromise(sql2);
    console.log(result2);
    res.json({
        message: `更新分类成功`,
        code: 20000
    })
}
// 分类数据api接口
CateController.cate = async (req, res) => {
    let sql = `select * from category order by cate_id desc`;
    let result = await queryPromise(sql)
    result.forEach(item => {
        item[`add_date`] = dateFormat(item[`add_date`])
        item[`update_date`] = dateFormat(item[`update_date`], `YYYY-MM-DD`)
    })
    res.json(result)
}
// 展示添加分类
CateController.addCate = function (req, res) {
    let userInfo = JSON.parse(req.session.userInfo || `{}`);
    res.render(`addCate.html`, { userInfo })
}

// 删除分类
CateController.delete = async (req, res) => {
    let { cat_id } = req.body;
    let sql = `delete from category where cate_id= ${cat_id}`;
    try {
        let result = await queryPromise(sql);
        console.log(result);
        res.json({
            code: 20000,
            message: `成功`
        })
    } catch (err) {
        res.json({
            code: -1,
            message: `删除`
        })
    }
}


// 获取当前指定的分类
CateController.detail = async (req, res) => {
    const { cat_id } = req.query;
    let sql = `select * from category where cate_id =${cat_id}`;
    try {
        let data = await queryPromise(sql)
        res.json(data[0] || {})
    } catch (err) {
        res.json({})
    }
}




// 添加分类入库api接口
CateController.insertCate = function (req, res) {
    let { is_show, cate_name } = req.body;
    let sql = `select * from category where cate_name ='${cate_name}'`
    queryPromise(sql).then(result => {
        if (result.length) {
            res.json({
                message: `分类名占用`,
                code: -1
            })
        } else {
            let add_date = getNowDate();
            let update_date = dateFormat();
            let sql2 = `insert into category(cate_name,is_show,add_date,update_date)
                       values('${cate_name}','${is_show}','${add_date}','${update_date}')`
            queryPromise(sql2).then(result => {
                if (result.affectedRows) {
                    res.json({
                        message: `添加分类成功`,
                        code: 20000
                    })

                }
            })
        }
    })
}
module.exports = CateController;